package com.bjmashibing.system.huawei.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjmashibing.system.huawei.SocketNIO;
import com.bjmashibing.system.huawei.jdbc.SpringJdbcTemplate;
import com.bjmashibing.system.huawei.static_var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Classname HelloController
 * @Description TODO
 * @Date 2021/2/15/015 16:23
 * @Created by Administrator
 */
@Controller
public class HelloController {
    private final static Logger logger = LoggerFactory.getLogger(HelloController.class);
//    @RequestMapping({"/","/index.html"})
//    public String index(){
//        return "index";
//    }

    @ResponseBody
    @RequestMapping("/hello_vivo")
//json
public String Hello_vivo(@RequestBody String avroInfo_vivo) {
        try{
            //    logger.info("avroInfo_vivo:" + avroInfo_vivo);
            JSONArray jsonArr = JSONObject.parseArray(avroInfo_vivo);
            for (int i = 0; i < jsonArr.size(); i++) {
                JSONObject obj = (JSONObject) jsonArr.get(i);
                Map<String, String> map_oppo = new HashMap<String, String>();
                map_oppo = JSONObject.parseObject(obj.toJSONString(), Map.class);
                logger.info("appId=" + map_oppo.get("appId"));
                logger.info("registrationIds=" + map_oppo.get("registrationIds"));
                String registrationIds = map_oppo.get("registrationIds");
                handle_receipt_oppo_xiaomi(registrationIds,1);
            }

            return "Hello World";
        }
        catch(Exception e){
            logger.info("Hello_vivo,error=",e);
            e.printStackTrace();
        }
        return "Hello World";
}
@ResponseBody
@RequestMapping("/hello_oppo")
//json
public String Hello_oppo(@RequestBody String avroInfo_oppo) {
    try{
        //    logger.info("avroInfo_oppo:" + avroInfo_oppo);
        JSONArray jsonArr = JSONObject.parseArray(avroInfo_oppo);
        for (int i = 0; i < jsonArr.size(); i++) {
            JSONObject obj = (JSONObject) jsonArr.get(i);
            Map<String, String> map_oppo = new HashMap<String, String>();
            map_oppo = JSONObject.parseObject(obj.toJSONString(), Map.class);
//        logger.info("appId=" + map_oppo.get("appId"));
//        logger.info("registrationIds=" + map_oppo.get("registrationIds"));
            String registrationIds = map_oppo.get("registrationIds");
            handle_receipt_oppo_xiaomi(registrationIds,1);
        }
        return "Hello World";
    }
    catch(Exception e){
        logger.info("main,error=",e);
        e.printStackTrace();
    }
    return "Hello World";

}

    @ResponseBody
    @RequestMapping("/hello_samsung")//huawei
    //json
    public String Hello_samsung(@RequestBody String avroInfo) {
        try{
            //        logger.info("avroInfo:" + avroInfo);
            JSONObject jsonObject = JSONObject.parseObject(avroInfo);
//        logger.info("jsonObject=" + jsonObject);
            JSONArray statuses = jsonObject.getJSONArray("statuses");
            SpringJdbcTemplate template = new SpringJdbcTemplate();
            for (int i = 0; i < statuses.size(); i++) {
                JSONObject obj = (JSONObject) statuses.get(i);
                Map<String, Object> map_huawei = new HashMap<String, Object>();
                map_huawei = JSONObject.parseObject(obj.toJSONString(), Map.class);
//            logger.info("token=" + map_huawei.get("token"));
//            logger.info("status=" + map_huawei.get("status"));
                Integer int_status = Integer.valueOf(map_huawei.get("status").toString());
//            logger.info("int_status=" + int_status);
                if ((int_status == 2)//应用未安装
                        || (int_status == 5)//指定的Token在当前Android终端用户下不存在
                        || (int_status == 6)//通知栏消息不展示
                        || (int_status == 10)//非活跃设备消息
                        || (int_status == 201)) //消息发送管控

                {
                    Integer len_deviceToken = map_huawei.get("token").toString().length();
                    Integer ii = 0;
                    String str_temp = "";
                    while (ii < (163 - len_deviceToken)) {
                        str_temp = str_temp + "0";
                        ii++;
                    }
                    String str_token = map_huawei.get("token").toString() + str_temp;
//                logger.info("Hello_samsung,str_token=" +str_token);
                    SpringJdbcTemplate springJdbcTemplate = new SpringJdbcTemplate();
                    springJdbcTemplate.token_queryForMap_huawei_receipt(str_token);
                }
            }
            return "Hello World";
        }
        catch(Exception e){
            logger.info("Hello_samsung,error=",e);
            e.printStackTrace();
        }
        return "Hello World";
    }
    @ResponseBody
    @RequestMapping("/hello_xiaomi")
    //form-urlencoded
    public String Hello_xiaomi(@RequestParam Map<String, String> params) {
        try{
            String data=params.get("data");
//        logger.info("data:" + data);
            JSONObject jsonObject = JSONObject.parseObject(data);
            Map<Object, Object> map2 = new HashMap<Object, Object>();
            map2 = JSONObject.parseObject(data, Map.class);
            for(Map.Entry<Object, Object> entry : map2.entrySet()){
//            logger.info("Key = "+entry.getKey()+",value="+entry.getValue());
                Map map = JSONObject.parseObject(entry.getValue().toString(), Map.class);
                String str_targets = map.get("targets").toString();
                handle_receipt_oppo_xiaomi(str_targets,0);
            }
            return "Hello World";
        }
        catch(Exception e){
            logger.info("Hello_xiaomi,error=",e);
            e.printStackTrace();
        }
        return "Hello World";
    }

    public static void handle_receipt_oppo_xiaomi(String str_targets,int int_brand) {
        try{
            if (int_brand == 0) {
                if (str_targets.length() > 64) {
                    int indexStart = 0;
                    //获取查找字符串的长度，这里有个规律：第二次查找出字符串的起始位置等于 第一次ab字符串出现的位置+ab的长度
                    String compareStr = ",";
                    int compareStrLength = compareStr.length();
                    int count = 0;
                    while (true) {
                        int tm = str_targets.indexOf(compareStr, indexStart);
                        if (tm >= 0) {
                            count++;
                            indexStart = tm + compareStrLength;
                        } else {
                            break;
                        }
                    }
                    for (int i = 0; i < count + 1; i++) {
                        String str_token = str_targets.substring(i * count + 64 + compareStrLength * i, i * 64 + compareStrLength * i);
//                    logger.info("str_token=" + str_token);
                    }
                } else if (str_targets.length() == 64) {

                    String str_token = str_targets;
//                logger.info("str_token=" + str_token);
                    Integer len_deviceToken = 64;
                    Integer ii = 0;
                    String str_temp = "";
                    while (ii < (163 - len_deviceToken)) {
                        str_temp = str_temp + "0";
                        ii++;
                    }
                    str_token = str_token + str_temp;
//                logger.info("str_token=" + str_token);
                    SpringJdbcTemplate springJdbcTemplate = new SpringJdbcTemplate();
                    springJdbcTemplate.token_update_receipt(str_token, 1);
                }
            }else if (int_brand == 1) {
                if (str_targets.length() > 40) {
                    int indexStart = 0;
                    //获取查找字符串的长度，这里有个规律：第二次查找出字符串的起始位置等于 第一次ab字符串出现的位置+ab的长度
                    String compareStr = ",";
                    int compareStrLength = compareStr.length();
                    int count = 0;
                    while (true) {
                        int tm = str_targets.indexOf(compareStr, indexStart);
                        if (tm >= 0) {
                            count++;
                            indexStart = tm + compareStrLength;
                        } else {
                            break;
                        }
                    }
                    for (int i = 0; i < count + 1; i++) {
                        String str_token = str_targets.substring(i * count + 35 + compareStrLength * i, i * 35 + compareStrLength * i);
//                    logger.info("str_token=" + str_token);
                    }
                } else if (str_targets.length() == 40) {

                    String str_token = str_targets;
                logger.info("str_token=" + str_token);
                    /*Integer len_deviceToken = 35;
                    Integer ii = 0;
                    String str_temp = "";
                    while (ii < (163 - len_deviceToken)) {
                        str_temp = str_temp + "0";
                        ii++;
                    }
                    str_token = str_token + str_temp;
//                logger.info("str_token=" + str_token);
                    SpringJdbcTemplate springJdbcTemplate = new SpringJdbcTemplate();
                    springJdbcTemplate.token_update_receipt(str_token, 1);*/
                }
            }
            else if (int_brand == 2) {
                if (str_targets.length() > 163) {
                    int indexStart = 0;
                    //获取查找字符串的长度，这里有个规律：第二次查找出字符串的起始位置等于 第一次ab字符串出现的位置+ab的长度
                    String compareStr = ",";
                    int compareStrLength = compareStr.length();
                    int count = 0;
                    while (true) {
                        int tm = str_targets.indexOf(compareStr, indexStart);
                        if (tm >= 0) {
                            count++;
                            indexStart = tm + compareStrLength;
                        } else {
                            break;
                        }
                    }
                    for (int i = 0; i < count + 1; i++) {
                        String str_token = str_targets.substring(i * count + 163 + compareStrLength * i, i * 163 + compareStrLength * i);
//                    logger.info("str_token=" + str_token);
                    }
                } else if (str_targets.length() == 163) {

                    String str_token = str_targets;
//                logger.info("str_token=" + str_token);
                    Integer len_deviceToken = 163;
                    Integer ii = 0;
                    String str_temp = "";
                    while (ii < (163 - len_deviceToken)) {
                        str_temp = str_temp + "0";
                        ii++;
                    }
                    str_token = str_token + str_temp;
//                logger.info("str_token=" + str_token);
                    SpringJdbcTemplate springJdbcTemplate = new SpringJdbcTemplate();
                    springJdbcTemplate.token_update_receipt(str_token, 1);
                }
            }
        }
        catch(Exception e){
            logger.info("main,error=",e);
            e.printStackTrace();
        }

    }

}
